package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserManagementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.UserManagement;
import com.itheima.service.UserManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = UserManagementService.class)//指定类
@Transactional//开启事务管理
public class UserManagementImpl implements UserManagementService {
    @Autowired
    private UserManagementDao userManagementDao;

    public void add(UserManagement userManagement,Integer[] roleIds,String staus,String sex) {

        userManagementDao.add(userManagement);
        Integer userManagementId = userManagement.getId();
        this.setUserManagementAndRole(userManagementId,roleIds,staus,sex);
    }

    //用户分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //使用分页插件获得
        PageHelper.startPage(currentPage,pageSize);

        Page<UserManagement> page = userManagementDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<UserManagement> rows = page.getResult();
        return new PageResult(total,rows);

    }

    //编辑用户
    @Override
    public void edit(UserManagement userManagement, Integer[] roleIds,String staus,String sex) {
        //修改基本信息
        userManagementDao.edit(userManagement);
        //把原来关联关系清除
        userManagementDao.deleteAssocication(userManagement.getId());
        //建立关联关系
        Integer userManagementId = userManagement.getId();
        this.setUserManagementAndRole(userManagementId,roleIds,staus,sex);

    }

    //根据id查询用户
    @Override
    public UserManagement findById(Integer id) {
        return userManagementDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserManagementId(Integer id) {
        return userManagementDao.findRoleIdsByUserManagementId(id);
    }

    @Override
    public void delete(Integer id) {
        userManagementDao.deleteAssocication(id);
        userManagementDao.delete(id);
    }

    @Override
    public List<UserManagement> findAll() {
        return userManagementDao.findAll();
    }

    public void setUserManagementAndRole(Integer userManagementId ,Integer[]roleIds,String staus,String sex){
        if (roleIds!=null&&roleIds.length>0){
            for (Integer roleId:roleIds) {
                Map<String,Object> map = new HashMap<>();
                map.put("userId",userManagementId);
                map.put("roleId",roleId);
                map.put("staus",staus);
                map.put("sex",sex);
                userManagementDao.setUserManagementAndRole(map);

            }
        }

    }



























}
